package com.logistics.distribution;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Distributiondatabase;

public class Categorylv2servlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	Distributiondatabase ddb;
	public Categorylv2servlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	       String type = request.getParameter("type");
	       String name1=new String(request.getParameter("name1").getBytes("iso-8859-1"),"utf-8");
	       System.out.println("gettype="+type);
	       if(type.equals("delete"))
	       {
	    	   System.out.println("type=delete");
	    	   String name2=new String(request.getParameter("name2").getBytes("iso-8859-1"),"utf-8");
	    	   System.out.println("name1="+name1+" name2="+name2);
	    	   ddb.categoryLevel2Delete(name1, name2);
	       }
	       else if(type.equals("insert"))
	       {
	    	   System.out.println("type=insert");
	    	   String name2=new String(request.getParameter("name2").getBytes("iso-8859-1"),"utf-8");
	    	   System.out.println("name1="+name1+" name2="+name2);
	    	   ddb.categoryLevel2Insert(name1, name2);
	    	   

	       }
	       ResultSet rs1=ddb.categoryLevel1Query();
	       ArrayList<String> category1 = new ArrayList<String>();
	       try {
	    	   while(rs1.next())
			   {
	    		   category1.add(rs1.getString(1));
			   }
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	       
	       request.setAttribute("category1", category1);
		       
	       ResultSet rs2=ddb.categoryLevel2Query(name1);
	       ArrayList<String> category2 = new ArrayList<String>();
	       try {
	    	   while(rs2.next())
			   {
				   category2.add(rs2.getString(2));
			   }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       for(int i=0;i<category2.size();i++)
	       {
	    	   System.out.println(category2.get(i));
	       }
	       request.setAttribute("category2", category2);
	       request.setAttribute("selectoption", name1);
	       request.getRequestDispatcher("../distribution/Categorylv2.jsp").forward(request, response); 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		ddb=new Distributiondatabase();
	}

}
